﻿#region File Description
//-----------------------------------------------------------------------------
// LoadGameScreen.cs
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using Microsoft.Xna.Framework;
using System;
using ManagerGame.GameSavingLogic;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace ManagerGame
{
    /// <summary>
    /// This screen shows all loaded games. Player can choose, which one to load
    /// </summary>
    class LoadGameScreen : MenuScreen
    {
        #region Fields
        MenuEntry savedGame;
        IAsyncResult result;
        #endregion

        #region Initialization

        string[] games;
        /// <summary>
        /// Constructor.
        /// </summary>
        public LoadGameScreen(string[] listOfSavedGames)
            : base("Load Game")
        {
            this.games = listOfSavedGames;
            // Create our menu entries.
            foreach (var game in listOfSavedGames)
            {
                savedGame = new MenuEntry(game);

                // Hook up menu event handler
                savedGame.Selected += loadGameSelected;

                // Add entries to the menu.
                MenuEntries.Add(savedGame);
            }

            MenuEntry back = new MenuEntry("Back");
            MenuEntry clearStorage = new MenuEntry("Delete Saved Games");
            back.Selected += OnCancel;

            clearStorage.Selected += deleteGamesMenuEntrySelected;

            MenuEntries.Add(clearStorage);
            MenuEntries.Add(back);
        }





        #endregion



        #region Handle Input

        // this is used when moving to BusinessManagingScreen
        int loadGameMenuEntryIndex = 0;
        protected override void OnSelectEntry(int entryIndex, PlayerIndex playerIndex)
        {
            loadGameMenuEntryIndex = entryIndex; // entry = 0 is "Buy..."
            base.OnSelectEntry(entryIndex, playerIndex);
        }

        /// <summary>
        /// Event handler for game loading
        /// </summary>
        void loadGameSelected(object sender, PlayerIndexEventArgs e)
        {

            for (int i = 0; i < games.Length; i++)
            {
                if (loadGameMenuEntryIndex == i)
                StartSelectedGame(e, games[i]);
            }
        }

        private void StartSelectedGame(PlayerIndexEventArgs e, string name)
        {
            RulesEngine.ResetCompanyIdCounters(Manager.Instance);
            Manager.InitializeManager();
            LoadingScreen.Load(ScreenManager, true, e.PlayerIndex,
                               new GameplayScreen(true, name)); // <- flag for loading a game (Manager object)
        }

        void deleteGamesMenuEntrySelected(object sender, PlayerIndexEventArgs e)
        { 
            StorageDevice device;
                result = StorageDevice.BeginShowSelector(
                                PlayerIndex.One, null, null);

                if (result.IsCompleted)
                {
                    device = StorageDevice.EndShowSelector(result);
                    GameSaver.DeleteSavedGameFiles(device);
                    string message = "Done!";
                    MessageBoxScreen gamesDeleted = new MessageBoxScreen(message, false);
                    ExitScreen();
                    ScreenManager.AddScreen(gamesDeleted, null);
                }
        }
        #endregion
    }
}
